import axios from 'axios';
// 引入 zarm 的 Toast 组件用于提示
import { Toast } from 'antd-mobile';
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';

// types.ts
export interface RequestInterceptors {
  // 请求拦截
  requestInterceptors?: (config: AxiosRequestConfig) => AxiosRequestConfig;
  requestInterceptorsCatch?: (err: any) => any;
  // 响应拦截
  responseInterceptors?: (config: AxiosResponse) => AxiosResponse;
  responseInterceptorsCatch?: (err: any) => any;
}
// 自定义传入的参数
export interface RequestConfig extends AxiosRequestConfig {
  interceptors?: RequestInterceptors;
}

class Request {
  // axios 实例
  instance: AxiosInstance;
  // 拦截器对象
  interceptorsObj?: RequestInterceptors;

  constructor(config: RequestConfig) {
    this.instance = axios.create(config);
    this.interceptorsObj = config.interceptors;

    this.instance.interceptors.request.use(
      (res: AxiosRequestConfig) => {
        return res;
      },
      (err: any) => err
    );

    // 使用实例拦截器
    this.instance.interceptors.request.use(
      this.interceptorsObj?.requestInterceptors,
      this.interceptorsObj?.requestInterceptorsCatch
    );
    this.instance.interceptors.response.use(
      this.interceptorsObj?.responseInterceptors,
      this.interceptorsObj?.responseInterceptorsCatch
    );
    // 全局响应拦截器保证最后执行
    this.instance.interceptors.response.use(
      // 因为我们接口的数据都在res.data下，所以我们直接返回res.data
      (res: AxiosResponse) => {
        return res.data;
      },
      (err: any) => err
    );
  }
}

/**
 * @description 导出获取数据方法
 * @param {String} url
 * @param {String} type
 * @param {Object} data
 * */

export function fetchData(url: string, type: string, data: any) {
  const ajaxType = type ? type.toLowerCase() : type;
  const config: AxiosRequestConfig = {};
  config.method = ajaxType || 'post';
  config.url = url;
  config.headers = {};

  if (ajaxType === 'get') {
    config.url = url + '?r=' + Math.random() * 1000;
    config.params = data;
  } else if (ajaxType === 'post') {
    config.data = data;
    config.headers['Content-Type'] = 'application/json;charset=UTF-8';
  }
  return axios(config);
}
